package com.anyou.mall.use.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.anyou.common.utils.RedisUtil;
import com.anyou.common.utils.uuid.RandomKit;
import com.anyou.mall.use.param.NoticeListParam;
import com.anyou.mall.use.param.UserChildParam;
import com.anyou.mall.use.param.UserIntegralParam;
import com.anyou.mall.use.vo.IntegralRecordVo;
import com.anyou.mall.use.vo.UserQuerySubordinateVo;
import com.anyou.mall.use.vo.UserQueryVo;
import com.anyou.mall.util.SMSUtil;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.anyou.common.annotation.Log;
import com.anyou.common.core.controller.BaseController;
import com.anyou.common.core.domain.ApiResult;
import com.anyou.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.anyou.mall.use.domain.User;
import com.anyou.mall.use.service.IUserService;
import com.anyou.common.utils.poi.ExcelUtil;
import com.anyou.common.core.page.TableDataInfo;

/**
 * 用户管理Controller
 *
 * @author gaochaohao
 * @date 2022-03-21
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/use/mallUser")
public class UserController extends BaseController
{
    @Autowired
    private IUserService userService;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 查询用户管理列表
     */
    @ApiOperation("查询用户管理列表")
    @PreAuthorize("@ss.hasPermi('use:mallUser:list')")
    @GetMapping("/list")
    public TableDataInfo<List<User>> list(User user)
    {
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }

//    /**
//     * 查询用户积分列表
//     */
//    @ApiOperation("分页查询用户积分列表")
//    @PreAuthorize("@ss.hasPermi('use:mallUser:IntegralList')")
//    @GetMapping("/userIntegralList")
//    public TableDataInfo<List<IntegralRecordVo>> userIntegralList(UserIntegralParam param)
//    {
//        startPage();
//        List<IntegralRecordVo> list = userService.userIntegralList(param);
//        return getDataTable(list);
//    }



    /**
     * 获取用户管理详细信息
     */
    @ApiOperation("获取用户管理详细信息")
    @PreAuthorize("@ss.hasPermi('use:mallUser:query')")
    @GetMapping(value = "/{userId}")
    public ApiResult<UserQueryVo> getInfo(@PathVariable("userId") Long userId)
    {
        return ApiResult.success(userService.selectUserByUserId(userId));
    }

    @ApiOperation("用户下级列表")
    @PreAuthorize("@ss.hasPermi('use:mallUser:userChildList')")
    @GetMapping(value = "/userChildList")
    public TableDataInfo<List<UserQuerySubordinateVo>> userChildList(@Valid UserChildParam param)
    {
        startPage();
        List<UserQuerySubordinateVo> list = userService.userChildList(param);
        return getDataTable(list);
    }
    @ApiOperation("用户积分明细")
    @PreAuthorize("@ss.hasPermi('use:mallUser:userJfList')")
    @GetMapping(value = "/userJfList")
    public TableDataInfo<List<IntegralRecordVo>> userJfList(@Valid UserChildParam param)
    {
        startPage();
        List<IntegralRecordVo> list = userService.userJfList(param);
        return getDataTable(list);
    }

    /**
     * 新增用户管理
     */
    /*@ApiOperation("新增用户管理")
    @PreAuthorize("@ss.hasPermi('use:mallUser:add')")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public ApiResult add(@RequestBody User user)
    {
        return ApiResult.success(userService.insertUser(user));
    }*/

    /**
     * 修改用户管理
     */
    /*@ApiOperation("修改用户管理")
    @PreAuthorize("@ss.hasPermi('use:mallUser:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public ApiResult edit(@RequestBody User user)
    {
        return ApiResult.success(userService.updateUser(user));
    }*/

    /**
     * 删除用户管理
     */
    @ApiOperation("删除用户管理")
    @PreAuthorize("@ss.hasPermi('use:mallUser:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public ApiResult remove(@PathVariable Long[] userIds)
    {
        return ApiResult.success(userService.deleteUserByUserIds(userIds));
    }

    /**
     * 修改用户状态
     */
    @ApiOperation(value = "修改用户状态")
    @PreAuthorize("@ss.hasPermi('use:mallUser:disable')")
    @Log(title = "用户管理",businessType = BusinessType.UPDATE)
    @PostMapping(value = "/{userId}")
    public ApiResult updateUserStatus(@PathVariable("userId") Long userId) {
        return userService.updateUserStatus(userId);
    }

    /**
     * 查询通知用户列表
     */
    @ApiOperation(value = "查询通知用户列表")
    @PreAuthorize("@ss.hasPermi('use:mallUser:noticeList')")
    @PostMapping(value = "/noticeList")
    public TableDataInfo<List<User>> noticeList(@RequestBody NoticeListParam noticeListParam) {
        startPage();
        List<User> list = userService.selectNoticeUserList(noticeListParam);
        return getDataTable(list);
    }

    /**
     * 发送短信验证码
     */
    @ApiOperation(value = "发送短信验证码")
    @PreAuthorize("@ss.hasPermi('use:mallUser:sendSmsCode')")
    @GetMapping(value = "/sendSmsCode")
    public ApiResult sendSmsCode(@RequestParam("phone") String phone) {
        int num6 = RandomKit.getNum6();
        String res = SMSUtil.sendSMS(phone, num6 + "");
        if(res.equals("success")){
            //将验证码存入redis,存5分钟
            redisUtil.setValue(phone,num6 + "",5*60);
        }
        return ApiResult.success();
    }
}
